/**
* 版权声明：厦门睿商网络科技有限公司 版权所有 违者必究
* 日    期：2020-12-15
*/
package com.rzico.article.controller.admin;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.rzico.annotation.Log;
import com.rzico.base.BaseController;
import com.rzico.base.CommResult;
import com.rzico.entity.Filter;
import com.rzico.entity.Pageable;
import com.rzico.entity.PageResult;
import com.rzico.article.entity.Talent;
import com.rzico.article.service.TalentService;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.Api;
import com.rzico.util.StringUtils;
import java.util.ArrayList;
import java.util.List;
import java.util.HashMap;
import java.util.Map;
import java.util.Date;

/**
 * 达人档案控制层
 *
 * @author Rzico Boot
 * @version 1.0
 * @date 2020-12-15
 */
@Api(description = "达人档案接口")
@RestController
@RequestMapping("/admin/talent")
public class TalentAdminController extends BaseController {

    @Autowired
    private TalentService talentService;

    /**
     * 分页查询达人档案
     *
     * @return
    */
    @ApiOperation("分页查询达人档案")
    @GetMapping("/list")
    @ApiImplicitParams({@ApiImplicitParam(name = "name", value = "昵称", dataType = "String", paramType = "query")
    })
    public CommResult<Talent> list(String name, Pageable pageable) {
        Map<String, Object> params = new HashMap<String, Object>();

        if (StringUtils.isNotEmpty(name)) {
            params.put("name", name);
        }

        Page<Object> startPage = PageHelper.startPage(pageable.getPageNum(), pageable.getPageSize());
        List<Talent> list = talentService.selectList(params);
        PageResult<Talent> pageResult = new PageResult<Talent>(list, startPage.getTotal(), pageable);
        return CommResult.success(pageResult);
    }

    /**
     * 查询单条达人档案
     *
     * @return
    */
    @ApiOperation("查询单条达人档案")
    @GetMapping("/find/{id}")
    public CommResult<Talent> find(@PathVariable String id) {
        Talent result = talentService.findById(id);

        Map<String,Object> data = new HashMap<>();
        data.put("talent",result);
        return CommResult.success(data);

    }

    /**
     * 保存达人档案
     *
     * @param talent
     * @return
     */
    @ApiOperation("保存达人档案")
    @PostMapping("/save")
    public CommResult<Talent> save(@RequestBody Talent talent) {
        int affectCount = talentService.insertUseGeneratedKeys(talent);
        if (affectCount <= 0) {
            return CommResult.error();
        }
        Talent result = talentService.findById(talent.getId());
        return CommResult.success(result);

    }


    /**
     * 批量删除达人档案
     *
     * @param ids
     * @return
     */
    @Log(desc = "批量删除达人档案", type = Log.LOG_TYPE.DEL)
    @ApiOperation("批量删除达人档案,ids用逗号拼接")
    @PostMapping("/del/{ids}")
    public CommResult<Talent> del(@PathVariable String ids) {

        talentService.deleteByIds(ids.split(","));
        return CommResult.success();
    }

    /**
     * 更新达人档案
     *
     * @param talent
     * @return
     */
    @Log(desc = "更新达人档案", type = Log.LOG_TYPE.UPDATE)
    @ApiOperation("更新达人档案")
    @PostMapping("/update")
    public CommResult<Talent> update(@RequestBody Talent talent) {
        int affectCount = talentService.updateByPrimaryKeySelective(talent);
        if (affectCount <= 0){
            return CommResult.error();
        }
        Talent result = talentService.findById(talent.getId());
        return CommResult.success(result);
    }

}
